#include <string.h>
#include "common.h"



int main(int argc, char ** argv)
{
	int i;
	FILE * instream = stdin;
	FILE * outstream = stdout;
	success_t succ = FALSE;

	for (i = 1; i < argc; i++)
	{
		if (strcmp(argv[i], "-i") == 0)
		{
			if (instream != stdin)
			{
				/* already given -i */
				goto cleanup;
			}
			i++;
			instream = fopen(argv[i], "r");
			if (instream == NULL)
			{
				/* failed to open file */
				goto cleanup;
			}
		}
		else if (strcmp(argv[i], "-o") == 0)
		{
			if (outstream != stdout)
			{
				/* already given -o */
				goto cleanup;
			}
			i++;
			outstream = fopen(argv[i], "w");
			if (outstream == NULL)
			{
				/* failed to open file */
				goto cleanup;
			}
		}
		else 
		{
			/* invalid switch */
			goto cleanup;
		}
	}
	
	printf("%p, %p\n", instream, stdin);

	succ = process_file(instream, outstream);

cleanup:
	if (instream != stdin && instream != NULL)
		fclose(instream);
	if (outstream != stdout && outstream != NULL)
		fclose(outstream);
	if (succ)
		return 0;
	else
		return 1;
}


